package com.ruoyi.erecord.employee.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.ruoyi.erecord.employee.domain.EmployeeImportDetail;
import com.ruoyi.erecord.employee.domain.bo.EmployeeImportDetailBo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeImportDetailVo;
import com.ruoyi.erecord.employee.mapper.EmployeeImportDetailMapper;
import com.ruoyi.erecord.employee.service.IEmployeeImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-15
 */
@RequiredArgsConstructor
@Service
public class EmployeeImportDetailServiceImpl implements IEmployeeImportDetailService {

    private final EmployeeImportDetailMapper baseMapper;

    /**
     * 查询人员导入明细
     */
    @Override
    public EmployeeImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询人员导入明细列表
     */
    @Override
    public TableDataInfo<EmployeeImportDetailVo> queryPageList(EmployeeImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeeImportDetail> lqw = buildQueryWrapper(bo);
        Page<EmployeeImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询人员导入明细列表
     */
    @Override
    public List<EmployeeImportDetailVo> queryList(EmployeeImportDetailBo bo) {
        LambdaQueryWrapper<EmployeeImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeImportDetail> buildQueryWrapper(EmployeeImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), EmployeeImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeCode()), EmployeeImportDetail::getEmployeeCode, bo.getEmployeeCode());
        lqw.like(StringUtils.isNotBlank(bo.getEmployeeName()), EmployeeImportDetail::getEmployeeName, bo.getEmployeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentityId()), EmployeeImportDetail::getIdentityId, bo.getIdentityId());
        lqw.eq(bo.getDeptId() != null, EmployeeImportDetail::getDeptId, bo.getDeptId());
        lqw.eq(bo.getProcessStatus() != null, EmployeeImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.eq(bo.getCreateTime() != null, EmployeeImportDetail::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增人员导入明细
     */
    @Override
    public Boolean insertByBo(EmployeeImportDetailBo bo) {
        EmployeeImportDetail add = BeanUtil.toBean(bo, EmployeeImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;

        return flag;
    }

    /**
     * 修改人员导入明细
     */
    @Override
    public Boolean updateByBo(EmployeeImportDetailBo bo) {
        EmployeeImportDetail update = BeanUtil.toBean(bo, EmployeeImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmployeeImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除人员导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public Boolean batchInsertBo(List<EmployeeImportDetailVo> voList) {
		List<EmployeeImportDetail> list= BeanCopyUtils.copyList(voList , EmployeeImportDetail.class);
		return baseMapper.insertBatch(list);
	}

	@Override
	public Boolean batchUpdatetBo(List<EmployeeImportDetailVo> voList) {
		List<EmployeeImportDetail> list= BeanCopyUtils.copyList(voList , EmployeeImportDetail.class);
		return baseMapper.updateBatchById(list);
	}
}
